#include <allegro.h>
#include "gui.h"
#include "main.h"
#include "colors.h"
#include "resources.h"

#include "main.h"
#include "resources.h"

#define INVBORDER_COL makecol(0x54,0x9e,0xc4)
#define INVBORDER_BG_COL makecol(0x00,0x39,0x6a)
#define HPBORDER_COL makecol(0xc7,0x59,0x50)
#define HPBG_COL makecol(0xb0,0x39,0x30)
#define HP_COL makecol(0xf0,0x10,0x10)

static void inventory_draw (Hud* h)
{
	Player* p = h->ref_player;
	int x = 8; // top left corner of inventory squares 5-group
	int y = HEIGHT-26;
	rectfill (buffer, x, y, x+5*18-1, y+17, INVBORDER_BG_COL);
	for (int i = 0; i < 5; i++) {
		rect (buffer, i*18+x, y, (i+1)*18+x-1, y+17, INVBORDER_COL);
		Item* item = &items[p->inv.slot[i]];
		masked_blit(bmp.icons, buffer, item->icon_x*16, item->icon_y*16, i*18+x+1, y+1, 16, 16);
	}
}

static void hp_draw (Hud* hud)
{
	int x = 8;
	int y = 8;
	int w = 102;
	int pxhp = (w-2) * hud->hp_display / hud->ref_player->skill.max_hp;
	int w_filled = MAX (0, pxhp);
	int h = 16;
	rectfill (buffer, x  , y  , x+w-1, y+h-1, HPBORDER_COL);
	rectfill (buffer, x+1, y+1, x+w-3, y+h-3, HPBG_COL);
	rectfill (buffer, x+1, y+1, x+w_filled-1, y+h-3, HP_COL);
}

Hud* hud_create(Player* p)
{
	Hud* h = malloc(sizeof(Hud));
	h->ref_player = p;
	h->hp_display = p->skill.max_hp;
	return h;
}

void hud_update(Hud* h)
{
	int hp_from = h->ref_player->hp;
	if (h->hp_display < hp_from) {
		h->hp_display++;
	} else if (h->hp_display > hp_from) {
		h->hp_display--;
	}
}

void hud_draw(Hud* h)
{
	inventory_draw (h);
	hp_draw (h);
}

void hud_free(Hud* h)
{
	free(h);
}
